//
//  UIViewController+Custom.m
//  Test
//
//  Created by Suny on 14-7-16.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "UIViewController+Custom.h"
#import <objc/runtime.h>
#import "BaseView.h"
#import "UIColor+BaseFramework.h"
#import "UIImage+BaseFramework.h"


@implementation UIViewController (Custom)
@dynamic maskView;

- (UIView *)maskView
{
	UIView *maskV = objc_getAssociatedObject(self,  @"UIViewController-maskView");
	if (maskV == nil)
	{
		maskV = [[UIView alloc] initWithFrame:self.view.bounds];
        maskV.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        maskV.userInteractionEnabled = YES;
		maskV.backgroundColor = [UIColor blackColor];

        if ([self.view isKindOfClass:[BaseView class]])
        {
            [(BaseView *)self.view suppressDelegateForAction:^{
                [self.view addSubview:maskV];
            }];
        }
        else
        {
            [self.view addSubview:maskV];
        }

		maskV.alpha = 0.0;
		self.maskView = maskV;
    }
	return maskV;
}

- (void)setMaskView:(UIView *)maskView
{
	objc_setAssociatedObject(self, @"UIViewController-maskView", maskView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NavigationBar *)navigationBar
{
	
	NavigationBar *_navigationBar = objc_getAssociatedObject(self,  @"UIViewController-NavigationBar");
	if (_navigationBar == nil)
	{
		
		_navigationBar = [[NavigationBar alloc] initWithFrame:CGRectMake(0,
																		 0,
																		 [UIScreen mainScreen].bounds.size.width,
																		 self.navigationBarHeight)];
		_navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		
		objc_setAssociatedObject(self, @"UIViewController-NavigationBar", _navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

	}
	return _navigationBar;
}

- (void)setupNavigationBar
{
	if (self.navigationBar.superview == nil)
	{
		[self.view addSubview:self.navigationBar];
		[self.navigationBar pushNavigationItem:self.navigationItem
								  animated:NO];
	}
	
	[self.navigationBar performSelectorOnMainThread:@selector(useCustomBackgroundWithColor:)
										 withObject:[UIColor colorWithRed:145/255.0
																	green:211/255.0
																	 blue:95/255.0
																	alpha:1.0]
									  waitUntilDone:YES];
	[self.navigationBar performSelectorOnMainThread:@selector(setCustomShadowImage:)
										 withObject:[UIImage createImageWithColor:[UIColor colorWithHexString:kNavigationBarShadowColor]]
									  waitUntilDone:YES];
	[self.view addSubview:self.navigationBar];
}


- (void)setNavigationBar:(NavigationBar *)navigationBar
{
	objc_setAssociatedObject(self, @"UIViewController-NavigationBar", navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setNavigationBarHeight:(CGFloat)navigationBarHeight
{
	objc_setAssociatedObject(self, @"UIViewController-navigationBarHeight",  [NSNumber numberWithFloat: navigationBarHeight],
							 OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}
- (CGFloat)navigationBarHeight
{
	return  [objc_getAssociatedObject(self,  @"UIViewController-navigationBarHeight") floatValue];
}


- (void)setHasNavigationBar:(BOOL)hasNavigationBar
{
	self.navigationBarHeight = hasNavigationBar ? kDefaultNavigationBarHeight : 0;
	
	objc_setAssociatedObject(self, @"UIViewController-hasNavigationBar", 
							 [NSNumber numberWithBool: hasNavigationBar], 
							 OBJC_ASSOCIATION_RETAIN_NONATOMIC);

	if (self.navigationBar)
	{
		self.navigationBar.hidden = !hasNavigationBar;
	}
	
	if (hasNavigationBar)
		{
		[self setupNavigationBar];
		
	}
	
}

- (BOOL)hasNavigationBar
{
	return [objc_getAssociatedObject(self,  @"UIViewController-hasNavigationBar") boolValue];
}






@end
